package com.zmc.product.controller;

import com.zmc.common.domain.good.GoodSkuBuyDetail;
import com.zmc.common.domain.vo.good.GoodsUpVo;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.product.application.GoodsApplicationService;
import com.zmc.product.infrastructure.bean.query.GoodsDetailQuery;
import com.zmc.product.infrastructure.entity.CommodityGoodsEntity;
import com.zmc.product.infrastructure.bean.query.CommodityGoodsQuery;
import com.zmc.product.infrastructure.service.CommodityGoodsService;
import com.zmc.product.infrastructure.bean.vo.GoodSpuVo;
import com.zmc.product.infrastructure.bean.vo.GoodsApproveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 商品管理
 * @author zmc
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private CommodityGoodsService commodityGoodsService;

    @Autowired
    private GoodsApplicationService goodsApplicationService;




    /**
     * 查询商品列表
     */
    @PostMapping("/list")
    public R list(@RequestBody CommodityGoodsQuery query) {
        PageUtils page = commodityGoodsService.pageCommodityGoodsList(query);
        return R.page(page);
    }


    /**
     * 导出商品列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityGoodsQuery query) {
        List<CommodityGoodsEntity> list = commodityGoodsService.selectCommodityGoodsList(query);
        ExcelUtil<CommodityGoodsEntity> util = new ExcelUtil<>(CommodityGoodsEntity.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取商品详细信息
     */
    @PostMapping(value = "/info")
    public R getInfo(@RequestBody GoodSpuVo vo) {
        return R.ok(goodsApplicationService.getGoodInfo(vo.getId()));
    }

    /**
     * 新增商品
     */
    @PostMapping("/add")
    public R add(@RequestBody GoodSpuVo vo) {
        goodsApplicationService.saveGood(vo);
        return R.ok();
    }

    /**
     * 修改商品
     */
    @PostMapping("/update")
    public R edit(@RequestBody GoodSpuVo vo) {
        goodsApplicationService.updateGood(vo);
        return R.ok();
    }

    /**
     * 删除商品
     */
    @PostMapping("/delete")
    public R remove(@RequestBody GoodSpuVo vo) {
        goodsApplicationService.remove(vo.getId());
        return R.ok();
    }

    /**
     * 提审商品
     */
    @PostMapping("/submit")
    public R submit(@RequestBody GoodsApproveVo approveVo) {
        goodsApplicationService.submit(approveVo);
        return R.ok();
    }

    /**
     * 审核商品
     */
    @PostMapping("/approve")
    public R approve(@RequestBody GoodsApproveVo approveVo) {
        goodsApplicationService.audit(approveVo);
        return R.ok();
    }

    /**
     * 上架商品
     */
    @PostMapping("/up")
    public R upGoods(@RequestBody List<GoodsUpVo> list) {
        goodsApplicationService.upGoods(list);
        return R.ok();
    }

    /**
     * 商品详情页
     * */
    @PostMapping("/detail")
    public R goodsDetail(@RequestBody GoodsDetailQuery query) {
        return R.ok(goodsApplicationService.getGoodsDetail(query));
    }

    /**
     * 计算商品体积、种类
     * */
    @PostMapping("/count/weight")
    public R countWeightVolume(@RequestBody List<GoodSkuBuyDetail> list){
        return R.ok(goodsApplicationService.countWeightVolume(list));
    }
}
